package org.aeon.colorfulplanebattle.view;


import org.aeon.colorfulplanebattle.constant.GameStateE;
import org.aeon.colorfulplanebattle.view.panel.MainPanel;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static org.aeon.colorfulplanebattle.constant.GameBasisConstant.*;

/**
 * @author: MissingAeon
 * @date: 2024/1/8 18:34
 * @description:
 */
public class GameUI extends JFrame {
    private final GameState gameState;

    public GameUI(GameState gameState) {
        this.gameState = gameState;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        this.setTitle(WINDOW_TITLE);
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setResizable(false);
        // 居中
        this.setLocationRelativeTo(null);
        // 添加主界面
        this.add(new MainPanel(this.gameState));
        // 请求焦点
        this.requestFocusInWindow();
        // 关闭窗口时
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                gameState.setState(GameStateE.FINISH);
            }
        });
    }
}
